const log4js = require('log4js');
export class Log4j{
    protected logger;  
    constructor(categoty){
        //获取当前时间以计算日志文件名
        var date = new Date();
        var time = date.getFullYear() + "-" + (date.getMonth() < 10 ? '0' + (date.getMonth()+1) : (date.getMonth()+1)) + "-" + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + "-" + date.getHours();  

        log4js.configure({
            appenders: [
                {type: 'console'},
                {type: 'file', filename: '/data0/thunderantslog/impression/impression-'+time, category: 'impression'},
                {type: 'file', filename: '/data0/thunderantslog/click/click-'+time, category: 'click'},
                {type: 'file', filename: '/data0/thunderantslog/like/like-'+time, category: 'like'},
                {type: 'file', filename: '/data0/thunderantslog/send/send-'+time, category: 'send'},
                {type: 'file', filename: '/data0/thunderantslog/comment/comment-'+time, category: 'comment'},
                {type: 'file', filename: '/data0/thunderantslog/default/default-'+time, category: 'default'}
            ]
        });
        if(categoty != 'impression' && categoty != 'click' && categoty != 'like' && categoty != 'send' && categoty != 'comment'){
            categoty = 'default';
        }
        this.logger = log4js.getLogger(categoty);
        this.logger.setLevel('INFO'); // 打印级别
        log4js.connectLogger(this.logger, { level: log4js.levels.INFO });
    }
    
    public log(data: any){
        if(typeof(data) == "object"){
            data = JSON.stringify(data);
        }
        this.logger.info(data);
    }
}

export default Log4j;
